package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //1.根据用户名查询数据库
        String username = employee.getUsername();
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("username",username);
        Employee one = employeeService.getOne(employeeQueryWrapper);
        if(one==null){
            return R.error("用户名不存在");
        }

        //2.比较密码是否正确
        //2.1 把参数中的密码进行md5加密
        String password = employee.getPassword();
        String pw1 = DigestUtils.md5DigestAsHex(password.getBytes());
        String pw2 = one.getPassword();

        if(!pw2.equals(pw1)){
            return R.error("密码错误");
        }
        //3.判断员工的状态（1表示启用，0禁用）
        if(one.getStatus()==0){
            return R.error("该用户已被禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    @PostMapping("logout")
    public R<String> logout(HttpSession session) {
        //销毁session
       session.invalidate();
        return R.success("退出成功");
    }

    @PostMapping
    private R<String> add(@RequestBody Employee employee,HttpSession session){
        //补缺相关数据
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));//设置默认密码
        //设置操作时间
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置操作人
        employee.setCreateUser((Long) session.getAttribute("employee"));
        employee.setUpdateUser((Long) session.getAttribute("employee"));*/

        employeeService.save(employee);
        return R.success("添加成功");
    }
    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        Page<Employee> page1 = new Page<>(page,pageSize);
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);

        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }

    @PutMapping
    public R update(@RequestBody Employee employee,HttpSession session){
       /* employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) session.getAttribute("employee"));*/
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 本来是写修改员工信息的 需要先根据ID查询信息并回显
     * 修改的方法在修改禁用启用的时候已经完成，前端跳转到修改界面
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
